// lista użytkowników systemu

unit Unit4;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, DBGrids, IBQuery, IBDatabase, ZDataset;

type

  { TForm4 }

  TForm4 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    ibq1: TIBQuery;
    ibq2: TIBQuery;
    ibt1: TIBTransaction;
    ibt2: TIBTransaction;
    mm: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form4: TForm4;

implementation

uses
   unit3, unit5, unit19;

{$R *.lfm}

{ TForm4 }


procedure TForm4.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if ibt1.Active then ibt1.Commit;
end;

procedure TForm4.FormShow(Sender: TObject);
var
  i : integer;
begin
  if ibt1.Active then ibt1.Commit;
  ibq1.SQL.Clear;
  ibq1.SQL.Add('select * from users');
  try
    ibq1.Transaction.StartTransaction;
    ibq1.Open;
    for i:=0 to DBGrid1.Columns.Count-1 do
    begin
      if pos('_IDX',UpperCase(DBGrid1.Columns[i].FieldName)) <> 0 then DBGrid1.Columns[i].Visible:=false;
    end;
  except
    ibq1.Transaction.Rollback;
    ShowMessage('Problem z wyswietleniem tabeli USERS');
  end;
end;

procedure TForm4.Edit2Change(Sender: TObject);
begin
end;

// edycja urzytkownika
procedure TForm4.Button2Click(Sender: TObject);
var
  idx : integer;
begin
  if not ibq1.Active then exit;
  idx:=ibq1.FieldByName('users_idx').AsInteger;

  form3.Caption:='Edycja urzytkownika';
  form3.Edit1.Text:=ibq1.FieldByName('ean').AsString;
  form3.Edit2.Text:=ibq1.FieldByName('nazwadl').AsString;
  form3.ActiveControl:=form3.Edit1;

  if form3.ShowModal = mrOk then
  begin
    if ibt2.Active then ibt2.Rollback;
    ibq2.SQL.Clear;
    ibq2.Params.Clear;
    ibq2.SQL.Add('update users set ean = :a, nazwadl = :b where users_idx = :c');
    ibq2.ParamByName('a').AsString:=form3.Edit1.Text;
    ibq2.ParamByName('b').AsString:=form3.Edit2.Text;
    ibq2.ParamByName('c').AsInteger:=idx;
    try
      ibq2.Transaction.StartTransaction;
      ibq2.ExecSQL;
      ibq2.Transaction.Commit;
    except
      ibq2.Transaction.Rollback;
      ShowMessage('Nie udalo się zmodyfikowac danych USERa');
    end;
  end;
  formshow(nil);
end;

procedure TForm4.Button3Click(Sender: TObject);
var
  idx : integer;
begin
  if not ibq1.Active then exit;
  idx:=ibq1.FieldByName('users_idx').AsInteger;

  if form5.ShowModal = mrOK then
  begin
    if ibt2.Active then ibt2.Rollback;
    ibq2.SQL.Clear;
    ibq2.Params.Clear;
    ibq2.SQL.Add('delete from users where users_idx = :a');
    ibq2.ParamByName('a').AsInteger:=idx;
    try
      ibq2.Transaction.StartTransaction;
      ibq2.ExecSQL;
      ibq2.Transaction.Commit;
    except
      ibq2.Transaction.Rollback;
      ShowMessage('Nie udalo sie usunac USERa');
    end;
  end;
  formshow(nil);
end;

// kopiowanie do schowka
procedure TForm4.Button4Click(Sender: TObject);
const
  delim = chr(9);
var
  x,row,col   : integer;
  y   : string;
//  mm  : TMemo;
begin

//  mm:=TMemo.Create(nil);
  mm.Clear;
  for x := 0 to DBGrid1.Columns.Count - 1 do
    Y := Y + (format('%s',[DBGrid1.columns[x].title.caption]))+ Delim;
  mm.Lines.Add(Y);

  for row := 0 to DBGrid1.DataSource.DataSet.RecordCount-1 do
  begin
    y:= '';
    for col := 0 to DBGrid1.Columns.Count-1 do  y:=y + DBGrid1.Columns[col].Field.AsString + #9;
    mm.Lines.Add(y);
    DBGrid1.DataSource.DataSet.Next;
  end;
  mm.SelectAll;
  mm.CopyToClipboard;
//  mm.Free;


end;

// dodanie nowego usera
procedure TForm4.Button1Click(Sender: TObject);
begin
  form3.Caption:='Nowy urzytkownik';
  form3.Edit1.Text:='';
  form3.Edit2.Text:='';
  form3.ActiveControl:=form3.Edit1;
  if form3.ShowModal = mrOK then
  begin
    if ibt2.Active then ibt2.Rollback;
    ibq1.SQL.Clear;
    ibq1.Params.Clear;
    ibq1.SQL.Add('insert into users (ean,nazwadl,data_zal) values (:a,:b,:c)');
    ibq1.ParamByName('a').AsString:=form3.Edit1.Text;
    ibq1.ParamByName('b').AsString:=form3.Edit2.Text;
    ibq1.ParamByName('c').AsDateTime:=now();
    try
      ibq1.ExecSQL;
    except
      ShowMessage('Nie moge dodać USERa');
    end;
  end;
  formshow(nil);
end;

procedure TForm4.Button5Click(Sender: TObject);
begin
  if ibq1.Active then ibq1.Close;
  close;
end;

end.

